package com.cxf.windex.rest.minab;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.biz.BOrdersBiz;
import com.cxf.windex.entity.BMeterials;
import com.cxf.windex.entity.BOrders;
import com.cxf.windex.entity.BShop;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @program: business_needs
 * @description: 订单
 * @author: chenxf
 * @create: 2018-10-17 16:58
 **/
@Api(tags = {"村广通[小程序-商] - 订单接口"})
@Controller
@RequestMapping("userb/orders")
public class BOrdersController extends BaseRestController<BOrdersBiz,BOrders> {
  @ApiOperation(value = "查询订单", notes = "查询订单")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "limit", value = "记录数 默认10",  paramType = "query", dataType = "int"),
          @ApiImplicitParam(name = "offset", value = "页码 默认 1",  paramType = "query", dataType = "int"),
          @ApiImplicitParam(name = "orderStr", value = "排序 默认desc",  paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/adminList", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<BOrders> adminList(
          @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
          @RequestParam(defaultValue = "desc") String orderStr,String status ) {
    List<BOrders> items = baseBiz.adminlist(limit, offset, orderStr,status);
    PageInfo<BOrders> pageInfo = new PageInfo<>(items);
    return new TableResultResponse<>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
            offset, limit,
            items);
  }
  @ApiOperation(value = "开启订单任务", notes = "开启订单任务")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "id", value = "订单ID", required = true, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/start/{id}", method = RequestMethod.PUT)
  @ResponseBody
  public ObjectRestResponse start(@PathVariable String id){
    baseBiz.start(id);
    return new ObjectRestResponse("删除成功").rel(true);
  }

  @ApiOperation(value = "删除订单信息", notes = "管理员删除未开始平台订单")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "id", value = "订单ID", required = true, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE)
  @ResponseBody
  public ObjectRestResponse del(@PathVariable String id){
    baseBiz.del(id);
    return new ObjectRestResponse("删除成功").rel(true);
  }

  @ApiOperation(value = "查询订单", notes = "查询订单")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "limit", value = "记录数 默认10", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "offset", value = "页码 默认 1", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "orderStr", value = "排序 默认desc", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "shopId", value = "店铺ID", required = true, paramType = "query", dataType = "long")
  })
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<BOrders> list(
      @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
      @RequestParam(defaultValue = "desc") String orderStr,Long shopId,String status ) {
    List<BOrders> items = baseBiz.list(limit, offset, orderStr,status,shopId);
    PageInfo<BOrders> pageInfo = new PageInfo<>(items);
    return new TableResultResponse<BOrders>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
        offset, limit,
        items);
  }

  @ApiOperation(value = "获取订单信息", notes = "根据ID获取订单信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "订单ID", required = true, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse getInfo(String id){
    BOrders entity=baseBiz.getInfo(id);
    if(entity==null){
      return new ObjectRestResponse("订单不存在").rel(false);
    }
    return new ObjectRestResponse("查询成功").rel(true).result(entity);
  }

  @ApiOperation(value = "保存订单", notes = "保存订单")
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse save(BOrders entity){
    return new ObjectRestResponse("保存成功").rel(true).result(baseBiz.save(entity));
  }

  @ApiOperation(value = "发布订单", notes = "发布订单(支付成功)")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "订单ID", required = true, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "totalMoney", value = "订单总金额", required = true, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/suc", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse suc(String id,String totalMoney){
    baseBiz.suc(id,totalMoney);
    return new ObjectRestResponse("发布成功").rel(true);
  }
}
